<?php

	if(!$_SESSION['user'] || !(unserialize($_SESSION['user']) instanceOf User))
	{
		$this->setGuiMode('ajax');
		header('Location: /blog/admin/login');
		return;
	}
	
	if($_POST['save'])
	{
		if($_POST['draft'] == 'on')
			$draft = 1;
		else
			$draft = 0;
			
		$args = array(
			'title' => $_POST['title'],
			'body' => $_POST['body'],
			'author' => 1,
			'date_added' => time(),
			'draft' => $draft
		);
		$post = new Post($args);
		$id = PostMapper::get()->insert($post);
		if($id)
		{
			$this->setGuiMode('ajax');
			header('Location: /blog/admin/manage');
			return;
		}
	}
	else
	{
		$post = new Post(array('draft' => 1));
	}
	
	$this->view()->assign('form_action', '/blog/admin/post/add');
	$this->view()->assign('post', $post);
	$this->addInclude('app/css/areas/admin/post/form.css','css');
?>